package com.jyy.prefabricated.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jyy.prefabricated.common.AjaxResult;
import com.jyy.prefabricated.dto.ChannelDto;
import com.jyy.prefabricated.dto.OrderDto;
import com.jyy.prefabricated.dto.PayAccountDto;
import com.jyy.prefabricated.entity.Channel;
import com.jyy.prefabricated.entity.OrderInfo;
import com.jyy.prefabricated.entity.PayAccount;
import com.jyy.prefabricated.service.PayAccountService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 支付账号表(PayAccount)表控制层
 *
 * @author mrl
 * @since 2021-08-16 14:47:47
 */
@Slf4j
@RequestMapping("/api")
@RestController
public class PayAccountController {
    /**
     * 服务对象
     */
    @Resource
    private PayAccountService payAccountService;

    /**
     * 新增商户
     * @param payAccount
     * @return
     */
    @PostMapping("/payAccount")
    public AjaxResult insert(@Valid @RequestBody PayAccount payAccount){
        log.info("Rest request to insert payAccount:{}",payAccount);
        payAccount.setFlag(0);
        PayAccountDto payAccountDto = new PayAccountDto();
        payAccountDto.setFlag(0);
        payAccountDto.setTeamCode(payAccount.getTeamCode());
        List<PayAccount> czAccount = payAccountService.selectByParams(payAccountDto);
        if(czAccount != null && czAccount.size() > 0){
            return AjaxResult.error("已存在改租户编号的商户信息！");
        }
        PayAccount result = payAccountService.insert(payAccount);
        return AjaxResult.success(result);
    }
    /**
     * 更新payAccount
     * @param payAccount
     * @return
     */
    @PutMapping("/payAccount")
    public AjaxResult update(@Valid @RequestBody PayAccount payAccount){
        log.info("Rest request to update payAccount:{}",payAccount);
        payAccountService.update(payAccount);
        PayAccount result = payAccountService.queryById(payAccount.getId());
        return AjaxResult.success(result);
    }
    /**
     * 查询payAccount详情
     * @param id
     * @return
     */
    @GetMapping("/payAccount/{id}")
    public AjaxResult selectById(@PathVariable Integer id){
        log.info("Rest request to find payAccount by id:{}",id);
        PayAccount result = payAccountService.queryById(id);
        return AjaxResult.success(result);
    }
    /**
     * 分页查询商户
     * @param payAccountDto
     * @return
     */
    @GetMapping("/payAccount")
    public AjaxResult selectByParams(PayAccountDto payAccountDto){
        log.info("Rest request to find channelDto by params payAccountDto", payAccountDto);
        PageHelper.startPage(payAccountDto.getPageNum(), payAccountDto.getPageSize());
        payAccountDto.setFlag(0);
        List<PayAccount> payAccount = payAccountService.selectByParams(payAccountDto);
        PageInfo<PayAccount> result = new PageInfo<>(payAccount);
        return AjaxResult.success(result);
    }
    /**
     * 删除payAccount
     * @param id
     * @return
     */
    @DeleteMapping("/payAccount/{id}")
    public AjaxResult deleteById(@PathVariable Integer id){
        log.info("Rest request to batch-delete payAccount by id:{}",id);
        boolean row = payAccountService.deleteById(id);
        if(row){
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }
}